package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void addDish(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);

        dishMapper.insertDish(dish);

        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors == null) return;
        flavors.forEach(flavor -> flavor.setDishId(dish.getId()));
        dishFlavorMapper.insertDishFlavorBatch(flavors);

    }

    @Override
    public PageResult getPage(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> page = (Page<DishVO>) dishMapper.getPage(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void deleteDishs(List<Long> ids) {
        int count = dishMapper.countByStatus(ids);
        if (count > 0) throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        count = setmealDishMapper.countByDishId(ids);
        if (count > 0) throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        dishMapper.deleteBatch(ids);
        dishFlavorMapper.deleteBatch(ids);
    }

    @Override
    public DishVO getDish(Long id) {
        DishVO vo = new DishVO();
        Dish dish = dishMapper.getDish(id);
        BeanUtils.copyProperties(dish, vo);
        List<DishFlavor> list = dishFlavorMapper.getByDishId(id);
        vo.setFlavors(list);
        return vo;
    }

    @Override
    public void updateDish(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.updateDish(dish);

        //Arrays.asList(dto.getId())
        dishFlavorMapper.deleteBatch(Arrays.asList(dto.getId()));
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> flavor.setDishId(dto.getId()));
            dishFlavorMapper.insertDishFlavorBatch(flavors);
        }
    }

    @Override
    public List<DishVO> getDishesByCategoryId(Long categoryId) {
        return dishMapper.getDishesByCategoryId(categoryId, null);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        dishMapper.updateDish(Dish.builder().id(id).status(status).build());
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Long categoryId) {
        List<DishVO> dishVOList = dishMapper.getDishesByCategoryId(categoryId, StatusConstant.ENABLE);
        for (DishVO vo : dishVOList) {
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(vo.getId());
            vo.setFlavors(flavors);
        }
        return dishVOList;
    }
}
